<?php
class Relation extends MrMongoDocument {
	public $friendId;
	public $followerId;
	public $relationType;//1,fan;2,blacklist
	public function getCollectionName ()
    {
        return 'relations';
    }
    
	public function primaryKey() {
		return 'friendId,followerId';
	}
	
	public static function model($className = __CLASS__) {
		return parent :: model($className);
	}
	
	public static function findRelation($friendId,$followerId){
		
		return self::model()->findAllByAttributes(array('friendId'=>$friendId,'followerId'=>$followerId,'relationType'=>1));

	}
	
	public static function findAllByFriendId($friendId){
		return self::model()->findAllByAttributes(array('friendId'=>$friendId,'relationType'=>1));
	}
	
	public static function findAllByFollowerId($followerId){
		return self::model()->findAllByAttributes(array('followerId'=>$followerId,'relationType'=>1));
	}

	public static function addRelation($friendId,$followerId){

			self::model()->friendId=$friendId;
			self::model()->followerId=$followerId;
			self::model()->relationType=1;
			self::model()->save();
	}
		public static function removeRelation($friendId,$followerId){

			self::model()->friendId=$friendId;
			self::model()->followerId=$followerId;
			self::model()->relationType=1;
			self::model()->remove();
	}
	
		public static function add2Blacklist($friendId,$followerId){

			self::model()->friendId=$friendId;
			self::model()->followerId=$followerId;
			self::model()->relationType=2;
			self::model()->save();
	}
		public static function removefromBlacklist($friendId,$followerId){

			self::model()->friendId=$friendId;
			self::model()->followerId=$followerId;
			self::model()->relationType=2;
			self::model()->remove();
	}
}